package com.uqbar.pacman.imageComponents;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import com.uqbar.vainilla.exceptions.GameException;

public class CreatorDensityMap {
	
public boolean isBlack(int pixel) {
	  int margenDeConsideracion = 70;
	  int red = (pixel >> 16) & 0xff;
	  int green = (pixel >> 8) & 0xff;
	  int blue = (pixel) & 0xff;
	  return red <= margenDeConsideracion  && green <= margenDeConsideracion && blue <= margenDeConsideracion;
}
 
public int blanco(){
	int ir = 255;// red component 0...255
	int ig = 255;// green component 0...255
	int ib = 255;// blue component 0...255
	int ia = 0;// alpha (transparency) component 0...255
	int col = (ia << 24) | (ir << 16) | (ig << 8) | ib;
	return col;
}

private int negro(){
	int r = 0;// red component 0...255
	int g = 0;// green component 0...255
	int b = 0;// blue component 0...255
	int a = 0;// alpha (transparency) component 0...255
	int col = (a << 24) | (r << 16) | (g << 8) | b;
	return col;
}


public BufferedImage ProcessImage(String imagePath){
	
	BufferedImage img;
	try {
		img = ImageIO.read(CreatorDensityMap.class.getResource(imagePath));
	}
	catch(Exception e) {
		throw new GameException("The resource '" + imagePath + "' was not found");
	}		
	
	BufferedImage img2 =new BufferedImage(img.getWidth(),img.getHeight(),img.getType());
	
	for(int ix = 0; ix<img.getWidth();ix++){
		for(int iy=0; iy<img.getHeight();iy++){
			if(isBlack(img.getRGB(ix, iy))){
				img2.setRGB(ix, iy, this.blanco());
			}
			else{
				img2.setRGB(ix, iy, this.negro());
			}
		}
	}
	
	return img2;
}

}
